using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;

namespace NIH.SharePoint.Branding.Features.SP2013Branding
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("068bea4f-6503-42ea-ba18-2da63f1473c6")]
    public class SP2013BrandingEventReceiver : SPFeatureReceiver
    {
        private static string NIH_DFLT_MASTERPAGE = "_catalogs/masterpage/NIHseattle.master";
        private static string MOSS_DFLT_MASTERPAGE = "_catalogs/masterpage/seattle.master";
        private static string WEB_FEATURE_DEFID = "0f486454-f97e-4273-9f46-e8b20e431b7a";


        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = properties.Feature.Parent as SPWeb;
            if (web != null)
            {
                SetMasterPage(web);                
            }
        }

        private static void SetMasterPage(SPWeb web)
        {
            try
            {
                string webAppRelativePath = web.ServerRelativeUrl;
                if (!webAppRelativePath.EndsWith("/"))
                {
                    webAppRelativePath += "/";
                }
                web.CustomMasterUrl = webAppRelativePath + NIH_DFLT_MASTERPAGE;
                web.MasterUrl = webAppRelativePath + NIH_DFLT_MASTERPAGE;
                web.Update();
                //if (web.Webs.Count > 0)
                //{
                //    foreach (SPWeb subWeb in web.Webs)
                //    {
                //        ActivateBrandingFeature(subWeb);
                //    }
                //}
            }
            catch (Exception e)
            {
                if (web != null)
                {
                    throw new Exception(string.Format("Encountered Exception applying NIH Master Page in Web: [{0}]", web.ServerRelativeUrl + e.Message));
                }
            }
        }
        
        private static void ActivateBrandingFeature(SPWeb thisWeb)
        {
            bool featureExists = false;
            using (thisWeb)
            {
                foreach (SPFeature feature in thisWeb.Features)
                {
                    if (feature.DefinitionId.Equals(new Guid(WEB_FEATURE_DEFID)))
                    {
                        featureExists = true;
                    }
                }
            }
            if (!featureExists)
            {
                thisWeb.Features.Add(new Guid(WEB_FEATURE_DEFID), true);
                thisWeb.Update();
            }
        }


        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb site = properties.Feature.Parent as SPWeb;
            if (site != null)
            {
                UnsetMasterPage(site);
            }
        }

        private static void UnsetMasterPage(SPWeb oWeb)
        {
            string webAppRelativePath = oWeb.ServerRelativeUrl;
            if (!webAppRelativePath.EndsWith("/"))
            {
                webAppRelativePath += "/";
            }
            oWeb.MasterUrl = webAppRelativePath + MOSS_DFLT_MASTERPAGE;
            oWeb.CustomMasterUrl = webAppRelativePath + MOSS_DFLT_MASTERPAGE;
            oWeb.Update(); 

            //if (oWeb.Webs.Count > 0)
            //{
            //    foreach (SPWeb web in oWeb.Webs)
            //    {
            //        DeactivateBrandingFeature(web);
            //    }
            //}
        }
        
        private static void DeactivateBrandingFeature(SPWeb thisWeb)
        {
            bool featureExists = false;
            using (thisWeb)
            {
                foreach (SPFeature feature in thisWeb.Features)
                {
                    if (feature.DefinitionId.Equals(new Guid(WEB_FEATURE_DEFID)))
                    {
                        featureExists = true;
                    }
                }
            }
            if (featureExists)
            {
                thisWeb.Features.Remove(new Guid(WEB_FEATURE_DEFID));
                thisWeb.Update();
            }
        }
    }
}
